<?php
/**
 * @user:andy
 * @date:2021/7/3
 * @company:http://www.sjdphp.com
 * @desc:
 */

namespace app\admin\business;


use app\common\server\Json;

class RoleBus extends BusBase
{
    public static function insertOrUpdateAndDel($param, $type = 1)
    {
        if(isset($param['powers'])){
            // 获取所属权限Id
            $param['powers'] = implode(',',self::getPowers($param['powers']));
        }

        $model = self::getModelExample();

        ## 判断操作类型
        $str = '';
        switch ($type){
            case 1:
                $res = $model::create($param);
                $str = '新增';
                break;
            case 2:
                $res = $model::update($param);
                $str = '更新';
                break;
            default:
                $res = $model::update($param);
                $str = '删除';
                break;
        }

        if($res){
            return Json::success($str . '成功');
        }else{
            return Json::error($str . "失败");
        }


    }


    private static function getPowers($data){
        $res = array();

        foreach($data as $k){

            if(!empty($k['children'])){
                $res = self::getPowers($k['children']);
            }
            $res[] = $k['id'];
        }

        return $res;
    }
}